import { _decorator, Component, director, EditBox, Node } from 'cc';
import { HTTP } from '../loading/HTTP';
import { Global } from '../global';
const { ccclass, property } = _decorator;

@ccclass('register')
export class register extends Component {
    // 注册的账号
    @property(EditBox)
    account: EditBox = null;

    // 注册的密码
    @property(EditBox)
    password: EditBox = null;

    // 登录的账号
    @property(EditBox)
    loginAccount: EditBox = null;

    // 登录的密码
    @property(EditBox)
    loginPassword: EditBox = null;

    // http请求
    private http: HTTP = null;

    // 进入选服务页面
    @property(Node)
    entryGame: Node = null;

    // 快速注册节点
    @property(Node)
    quickRegister: Node = null;

    // 登录页面节点
    @property(Node)
    login: Node = null;

    protected onLoad(): void {
        this.http = Global.http;
    }

    // 注册
    onRegister() {
        // 获取输入框的值
        let account = this.account.string;
        let password = this.password.string;
        let data = {
            account,
            password
        }
        this.http.sendRequest('/register', 'GET', data, (res) => {
            if (res.code == 0) {
                Global.UsrMg.account = account;
                Global.UsrMg.sign = res.key;
                // 进入选游戏服务页面
                this.entryGame.active = true;
                this.quickRegister.active = false;
            } else {
                console.log(res.msg)
            }
        })
    }

    // 登录游戏
    loginGame() {
        let account = this.loginAccount.string;
        let password = this.loginPassword.string;

        let data = {
            account,
            password
        }
        this.http.sendRequest('/loginGame', 'GET', data, (res) => {
            if (res.code == 1) {
                console.log(res.msg);
                return;
            } else {
                Global.UsrMg.account = account;
                Global.UsrMg.sign = res.sign;
                Global.UsrMg.username = res.userInfo.username;
                Global.UsrMg.userid = res.userInfo.userid;
                Global.UsrMg.sex = res.userInfo.sex;
                Global.UsrMg.lv = res.userInfo.lv;
                Global.UsrMg.exp = res.userInfo.exp;
                Global.UsrMg.coins = res.userInfo.coins;
                Global.UsrMg.gems = res.userInfo.gems;
                Global.UsrMg.vip = res.userInfo.vip;
                Global.UsrMg.expAll = res.userInfo.expAll;
                Global.UsrMg.x = res.userInfo.x;
                Global.UsrMg.y = res.userInfo.y;
                this.entryGame.active = true;
                this.login.active = false;
            }
        })
    }

    // 游戏大厅
    loginGameHall() {
        if (Global.UsrMg.username) {
            director.loadScene('game'); // 游戏大厅
        } else {
            director.loadScene('nickName'); // 创建角色
        }
    }
}


